--- Day 11: Corporate Policy --- Santa's previous password expired, and he needs help choosing a new one. To help him remember his new password after the old one expires, Santa has devised a method of coming up with a password based on the previous one. Corporate policy dictates that passwords must be exactly eight lowercase letters (for security reasons), so he finds his new password by incrementing his old password string repeatedly until it is valid. Incrementing is just like counting with numbers: xx, xy, xz, ya, yb, and so on. Increase the rightmost letter one step; if it was z, it wraps around to a, and repeat with the next letter to the left until one doesn't wrap around. Unfortunately for Santa, a new Security-Elf recently started, and he has imposed some additional password requirements: Passwords must include one increasing straight of at least three letters, like abc, bcd, cde, and so on, up to xyz. They cannot skip letters; abd doesn't count. Passwords may not contain the letters i, o, or l, as these letters can be mistaken for other characters and are therefore confusing. Passwords must contain at least two different, non-overlapping pairs of letters, like aa, bb, or zz. For example: hijklmmn meets the first requirement (because it contains the straight hij) but fails the second requirement requirement (because it contains i and l). abbceffg meets the third requirement (because it repeats bb and ff) but fails the first requirement. abbcegjk fails the third requirement, because it only has one double letter (bb). The next password after abcdefgh is abcdffaa. The next password after ghijklmn is ghjaabcc, because you eventually skip all the passwords that start with ghi..., since i is not allowed. Given Santa's current password (your puzzle input), what should his next password be? Your puzzle input is vzbxkghb.

In [28]:
def find_three_straight(passwd):
    for a,b,c in zip(passwd[:-2], passwd[1:-1], passwd[2:]):
        a, b, c = ord(a), ord(b), ord(c)
        if a+2 == b+1 and a+2 == c:
            return chr(a) + chr(b) + chr(c)
        
    return None

def find_pairs(passwd):
    pairs = set()
    for a,b in zip(passwd[:-1], passwd[1:]):
        if a == b:
            pairs.add("{}{}".format(a, b))
    return pairs
    
def check_valid_password(passwd):
    
    if ('i' in passwd) or ('o' in passwd) or ('l' in passwd):
        return False
    
    if len(find_pairs(passwd)) < 2:
        return False
    
    if find_three_straight(passwd) is None:
        return False 
    
    return True 

def increase_password(passwd):    
    new_passwd = ""  
    inc = True
    for c in passwd[::-1]:
        if inc:
            r = ord(c) + 1
            if r > ord('z'):
                r = ord('a')
            else:
                inc = False
            c = chr(r)
        new_passwd = c + new_passwd
    return new_passwd

def next_password(passwd):
    
    next_passwd = increase_password(passwd)    
    while not check_valid_password(next_passwd):
        next_passwd = increase_password(next_passwd)    
    
    return next_passwd

In [31]:
%%time

# Test example
next_password("ghijklmn")


CPU times: user 38 s, sys: 3.89 ms, total: 38 s
Wall time: 38 s
Out[31]:
'ghjaabcc'

In [32]:
%%time

# Check input
next_password("vzbxkghb")


CPU times: user 2.2 s, sys: 0 ns, total: 2.2 s
Wall time: 2.2 s
Out[32]:
'vzbxxyzz'
--- Part Two --- Santa's password expired again. What's the next one? Your puzzle input is still vzbxkghb.

In [33]:
%%time

# Check input
next_password("vzbxxyzz")


CPU times: user 8.42 s, sys: 3.99 ms, total: 8.43 s
Wall time: 8.43 s
Out[33]:
'vzcaabcc'

In [ ]: